<template>
    <el-form :model="ruleForm"  ref="ruleForm" label-position="right"  label-width="100px" class="small-space">
          <h1 style="padding-left: 50px;margin-bottom: 50px" v-if="!this.$route.query.id">
            添加课程
          </h1>
          <h1 style="padding-left: 50px;margin-bottom: 50px" v-if="this.$route.query.id">
            编辑课程
          </h1>

      <el-row v-loading.body="listLoading" :gutter="24">
        <el-col :span="12" :offset="4">
          <el-form-item label="图片" prop="images_id" >
            <ele-upload
              :imageUrl="imageUrl"
              strategy="course"
              :showFileList="false"
              @on-success="handleAvatarSuccess">
            </ele-upload>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.mobile">{{errors.mobile ? errors.mobile[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="持续时间/分钟" prop="duration">
            <template>
              <el-input-number v-model="ruleForm.duration"  :min="1" :max="100"></el-input-number>
            </template>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.duration">{{errors.duration ? errors.duration[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.name">{{errors.name ? errors.name[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="视频地址" prop="video">
            <el-input v-model="ruleForm.video"></el-input>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.video">{{errors.video ? errors.video[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="适应人群" prop="suitable_people">
            <el-input type="textarea" v-model="ruleForm.suitable_people"></el-input>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.suitable_people">{{errors.suitable_people ? errors.suitable_people[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="介绍" prop="introduction">
            <tinymce class="edit_tinymce" v-model="ruleForm.introduction"></tinymce>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.introduction">{{errors.introduction ? errors.introduction[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="训练效果" prop="training_effect">
            <tinymce class="edit_tinymce" v-model="ruleForm.training_effect"></tinymce>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.training_effect">{{errors.training_effect ? errors.training_effect[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="faq" prop="faq">
            <tinymce class="edit_tinymce" v-model="ruleForm.faq"></tinymce>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.faq">{{errors.faq ? errors.faq[0] : ''}}</div>
            </transition>
          </el-form-item>
          <el-form-item label="备注" prop="notes">
            <tinymce class="edit_tinymce" v-model="ruleForm.notes"></tinymce>
            <transition name="el-zoom-in-top">
              <div class="el-form-item__error" v-if="errors.notes">{{errors.notes ? errors.notes[0] : ''}}</div>
            </transition>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="7">
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm()">重置</el-button>
            <el-button  type="primary" @click="returnPage">返回上一页</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  <!--</el-dialog>-->
</template>

<script>
import _ from 'lodash'
import { Show, Update, Create } from '@/api/course'
import EleUpload from '@/components/EleUpload'
import Tinymce from '@/components/Tinymce'

export default {
  components: {
    EleUpload,
    Tinymce
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String
    },
    editId: {}
  },
  data() {
    return {
      errors: {},
      listLoading: false,
      Visible: this.dialogVisible,
      loadTable: false,
      ruleForm: {
        name: null,
        duration: 1,
        introduction: null,
        video: null,
        images_id: null,
        training_effect: null,
        suitable_people: null,
        faq: null,
        notes: null
      },
      imageUrl: null
    }
  },
  watch: {
    // 监听弹框打开关闭
    /*   dialogVisible() {
      if (this.dialogVisible) {
        this.loadTable = false
        this.Visible = this.dialogVisible
        //          ↓ 打开的时候
        if (this.title === '编辑') {
          this.listLoading = true
          Show(this.editId).then((res) => {
            this.ruleForm = res.data
            this.imageUrl = res.data.image.data.url
            this.listLoading = false
          })
        }
      }
    },
    Visible() {
      this.$emit('closeDialog', this.loadTable, this)
    }*/
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.errors = {}
          this.listLoading = true
          if (this.$route.query.id) {
            //  this.ruleForm.introduction = this.ruleForm.introduction.replace(/\n|\r\n/g,"<br>")
            //  console.log(this.ruleForm.introduction, '保存的')
            Update(this.$route.query.id, _.pick(this.ruleForm, ['name', 'duration', 'introduction', 'video', 'training_effect', 'suitable_people', 'faq', 'notes', 'images_id'])).then(() => {
              this.$message({ message: '保存成功', type: 'success' })
              this.$router.push({ path: '/course' })
              this.close()
            }).catch((error) => {
              const { response } = error
              this.errors = response.data.errors
              this.listLoading = false
            })
          } else {
            Create(_.pick(this.ruleForm, ['name', 'duration', 'introduction', 'video', 'training_effect', 'suitable_people', 'faq', 'notes', 'images_id'])).then(() => {
              this.$message({ message: '保存成功', type: 'success' })
              this.$router.push({ path: '/course' })
              this.close()
            }).catch((error) => {
              const { response } = error
              this.errors = response.data.errors
              this.listLoading = false
            })
          }
        }
      })
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    close(v = false) {
      this.listLoading = false
      this.resetForm() // 清空表单
      if (!v) this.loadTable = true
      this.Visible = false // 关闭对话框
    },
    // 图片上传成功时
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.ruleForm.images_id = res.id
    },
    returnPage() {
      this.$router.push({ path: '/course' })
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.listLoading = true
      Show(this.$route.query.id).then((res) => {
        this.ruleForm = res.data
        this.imageUrl = res.data.image.data.url
        this.listLoading = false
      })
    }
  }
}
</script>
<style scoped>
  .classtitle{
    font-size: x-large;
    font-weight: 500;
  }
</style>
